package cn.online.taxi.api.passenger.controller;

import cn.online.taxi.common.pojo.Result;
import cn.online.taxi.common.utils.ResultUtil;
import cn.online.taxi.manager.dto.front.Order;
import cn.online.taxi.manager.pojo.TbCarInfo;
import cn.online.taxi.manager.service.service.CarMangerService;
import cn.online.taxi.order.dispatch.service.DispatchService;
import cn.online.taxi.order.service.service.OrderService;
import cn.online.taxi.valuation.service.ValuationService;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author BinBini
 * @Date 2021/04/19 10:36
 * @Version 1.0
 */
@RestController
@Api(value = "乘客订单")
public class OrderController {

    @Reference
    private DispatchService dispatchService;
    @Reference
    private ValuationService valuationService;
    @Reference
    private OrderService orderService;
    @Reference
    private CarMangerService carMangerService;

    @RequestMapping(value = "/passenger/order/forecast", method = RequestMethod.GET)
    @ApiOperation(value = "订单预测")
    public Result<Object> forecast(double startLongitude, double startLatitude, double targetLongitude, double targetLatitude) {
        Double price = valuationService.calcForecastPrice(startLongitude, startLatitude, targetLongitude, targetLatitude);
        return new ResultUtil<Object>().setData(price);
    }

    @RequestMapping(value = "/passenger/order/dispatch", method = RequestMethod.POST)
    @ApiOperation(value = "派单")
    public Result<Order> dispatch(Integer passengerInfoId, String passengerPhone, String userLongitude,
                                    String userLatitude, String startLongitude, String startLatitude,
                                    String startAddress, String endAddress, String endLongitude,
                                    String endLatitude, String passengerToken) {
        Order result = dispatchService.dispatch(passengerInfoId, passengerPhone, userLongitude, userLatitude, startLongitude, startLatitude, startAddress, endAddress, endLongitude, endLatitude, passengerToken);
        if(result == null) {
            return new ResultUtil<Order>().setErrorMsg("发起订单失败");
        }
        return new ResultUtil<Order>().setData(result);
    }

    @RequestMapping(value = "/getOrderRedis", method = RequestMethod.GET)
    @ApiOperation(value = "获取订单详情")
    public Result<Order> getOrderRedis(String orderNumber) {
        Order result = orderService.getOrderRedisByNumber(orderNumber);
        return new ResultUtil<Order>().setData(result);
    }

    @RequestMapping(value = "/checkCarInfo", method = RequestMethod.GET)
    @ApiOperation(value = "获取车辆详情")
    public Result<TbCarInfo> checkCarInfo(Integer carId) {
        TbCarInfo result = carMangerService.checkCarById(carId);
        return new ResultUtil<TbCarInfo>().setData(result);
    }



}
